package cn.zhentao.dao;

import cn.zhentao.dao.impl.TeacherDaoImpl;
import cn.zhentao.pojo.Teacher;
import org.junit.Test;

import java.sql.*;
import java.util.List;

public class JDBCTest2 {
    @Test
    public void testAdd() throws Exception {
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //创建连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/2405a","root","123456");
        //创建Statement对象
        Statement stmt = conn.createStatement();
        String sql = "insert into teacher values(null,'蒙奇D龙','123456','2023-05-05','13577799555','人妖岛')";
        //执行插入语句
        int i = stmt.executeUpdate(sql);
        System.out.println(i);
    }

    @Test
    public void testDel() throws Exception {
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //创建连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/2405a","root","123456");
        //创建Statement对象
        Statement stmt = conn.createStatement();
        String sql = "delete from teacher where tid=4";
        int i=stmt.executeUpdate(sql);
        System.out.println(i);
    }

    @Test
    public void testFindAll() throws SQLException {
        TeacherDao dao = new TeacherDaoImpl();
        List<Teacher> list =  dao.findAll();
        for(Teacher t:list){
            System.out.println(t);
        }
    }

    @Test
    public void testLogin() throws Exception {
        TeacherDao dao = new TeacherDaoImpl();
        Teacher t = dao.login("雷利","1' or '1'='1");
        System.out.println(t);
    }

    @Test
    public void testDelTeacher() throws SQLException {
        TeacherDao dao = new TeacherDaoImpl();
        int i  = dao.delTeacher(5);
        System.out.println(i);
    }

    //测试添加数据
    @Test
    public void testAddTeacher() throws SQLException {
        //创建Teacher对象
        Teacher t = new Teacher(null,"凯多","123456",new Date(System.currentTimeMillis()),
                "13577799888","和之国");
        TeacherDao dao = new TeacherDaoImpl();
        int i = dao.addTeacher(t);
        System.out.println(i);
    }

    //测试修改数据
    @Test
    public void testUpdateTeacher() throws SQLException {
        Teacher t = new Teacher(6,"可爱多","123456",new Date(System.currentTimeMillis()),
                "13577799888","鬼岛");
        TeacherDao dao = new TeacherDaoImpl();
        int i = dao.updateTeacher(t);
        System.out.println(i);
    }
}
